home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr52 / wildcard.zip / WILDCARD.PRG < prev   
Text File  |  1993-04-02  |  3KB  |  71 lines

  1. * Function:  [<logical exp>] = K_WILDCARD(<char exp1>,<char exp2>)
  2. * Params  :  <char exp1> is the target string to search
  3. *            <char exp2> is the mask to search the target with
  4. * Purpose :  Provide an alternative to concatenated SUBSTR() comparisons
  5. *            for the purpose of filtering databases primarily for SET
  6. *            FILTER TO or FOR/WHILE conditions.  Basically anywhere
  7. *            string comparisons are needed.
  8. * Example :  REPORT FORM test TO PRINT FOR k_wildcard(textfield,"?89?i?")
  9. * Returns :  a logical .T. or .F.
  10. * Author  :  Kim Bjork, AMG Inc., 12/23/90, CIS 74017,1576
  11. *         :  Released for public usage.  No warranty implied or granted.
  12. * Notes   : -<char exp1> can be any valid char string, ie. memvar or field.
  13. *           -<char exp2>, the mask, must be formed with question marks,
  14. *            ie. "??IS????", "32??????", "?whEre?iS??", etc.
  15. *            The example, "?whEre?iS??", is where this function makes
  16. *            a difference, otherwise, use SUBSTR().
  17. *            Of course, it will work without any wildcard characters
  18. *            but then why use it?
  19. *           -Any printable character other than a question mark
  20. *            is considered valid search criteria (a significant char).
  21. *           -If the mask string is longer than the target string, the
  22. *            mask string will be truncated to the length of the target.
  23. *            Processing will be slowed slightly if the mask string is
  24. *            longer than the target (more code executes).
  25. *           -The target and mask strings will be forced to upper case
  26. *            for the purpose of the search.
  27. *           -To speed up processing, remove the TYPE() checking portion
  28. *            of the code.  If you want to take responsibility at the 
  29. *            calling portion of your code, remove the mask length checking
  30. *            also.
  31. *           -This function in entirely inappropriate for large character
  32. *            strings.  Was designed for memvar lengths of 15 chars or
  33. *            less.  The longer the mask, the more iterations the 
  34. *            FOR..NEXT loop has to perform.
  35. *           -Designed for use with Clipper S'87, Clipper 5.0 version
  36. *            is in CLIP5VER.PRG.
  37.  
  38. function k_wildcard
  39.    parameters t_, m_              && target, mask
  40.    private tl, ml, i
  41.  
  42.    * check parameters
  43.    if type("t_") + type("m_") != "CC"
  44.       retu .F.
  45.    endif
  46.  
  47.    tl = len(t_)                   && target length
  48.    ml = len(m_)                   && mask length
  49.  
  50.    if ml > tl                     && mask longer than target?
  51.       ml = tl                     && mask length becomes same as target
  52.       m_ = substr(m_,1,tl)        && truncate to target length
  53.    endif
  54.  
  55.    * force to upper case
  56.    t_ = upper(t_)
  57.    m_ = upper(m_)
  58.  
  59.    * replace the target with "?" chars from the mask (where appropriate)
  60.    for i = 1 to ml
  61.       if substr(m_,i,1) = "?"     && not a significant char?
  62.          t_ = stuff(t_,i,1,"?")   && replace target char with "?"
  63.       endif
  64.    next
  65.  
  66.    * substring of target string same as the mask?
  67. return iif(substr(t_,1,ml) = m_, .T., .F.)
  68.  
  69. * I would be very interested in improvements to this function.
  70.  
  71.